package com.gec.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.model.entity.SysChargeItems;
import com.gec.model.vo.SysChargeItemsQueryVo;
import com.gec.system.service.SysChargeItemsService;
import com.gec.system.util.Constant;
import com.gec.system.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 收费项目表 前端控制器
 * </p>
 *
 * @author lys
 * @since 2023-12-07
 */
@Api(tags = "收费项目管理控制器")
@RestController
@RequestMapping("/admin/system/sysChargeItems/")
public class SysChargeItemsController {

    @Autowired
    private SysChargeItemsService sysChargeItemsService;

    @ApiOperation("分页查询收费项目列表")
    @GetMapping("{page}/{limit}")
    public Result findPage(@PathVariable Long page,
                           @PathVariable Long limit,
                           SysChargeItemsQueryVo vo){

        IPage<SysChargeItems> page1 = new Page<>(page,limit);
        page1 = this.sysChargeItemsService.selectPage(page1,vo);
        return Result.ok(page1);
    }

    @ApiOperation("添加收费项目")
    @PostMapping("addItem")
    public Result addItem(@RequestBody SysChargeItems sysChargeItems){
        String itemCode = getItemCode(sysChargeItems.getType(), sysChargeItems.getId());

        sysChargeItems.setCode(itemCode);
        boolean save = this.sysChargeItemsService.save(sysChargeItems);
        if (save){
            return Result.ok();
        }

        return Result.fail();
    }

    //自动生成项目编号
    public String getItemCode(Integer type, Long id) {
        int seed;
        String pre;
        switch (type){
            case 0:pre = "CG";
            break;
            case 1:pre = "LS";
                break;
            case 2:pre = "YJ";
                break;
            case 3:pre = "YS";
                break;
            default:pre="CG";
            break;
        }
        if (id==null){
            //添加
            int maxId = this.sysChargeItemsService.getMaxId();
            seed = (type+1)*10000+maxId+1;
            return Constant.getCode(pre,seed);
        }
        //修改
        //type+1防止type为0
         seed =(type+1)*10000+Integer.valueOf(id+"");
        return Constant.getCode(pre,seed);
    }

    @ApiOperation("根据id查询收费项目")
    @GetMapping("getItemById/{id}")
    public Result getItemById(@PathVariable Long id){
        QueryWrapper<SysChargeItems> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        SysChargeItems one = this.sysChargeItemsService.getOne(wrapper);
        return Result.ok(one);
    }

    @ApiOperation("根据id修改收费项目")
    @PostMapping("updateItemById")
    public Result updateItemById(@RequestBody SysChargeItems sysChargeItems){
        String itemCode = getItemCode(sysChargeItems.getType(), sysChargeItems.getId());

        sysChargeItems.setCode(itemCode);

        boolean b = this.sysChargeItemsService.updateById(sysChargeItems);
        if (b){
            return Result.ok();
        }
        return Result.fail();
    }


    @ApiOperation("根据id删除收费项目")
    @DeleteMapping("removeItemById/{id}")
    public Result removeCountById(@PathVariable Long id){

        boolean b = this.sysChargeItemsService.removeById(id);
        if (b){
            return Result.ok();
        }
        return Result.fail();
    }

    @ApiOperation("根据ids批量删除收费项目")
    @DeleteMapping("removeItemByIds")
    public Result removeItemByIds(@RequestBody List<Long> ids){

        boolean b = this.sysChargeItemsService.removeByIds(ids);
        if (b){
            return Result.ok();
        }
        return Result.fail();
    }


}

